﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utilities;
using SM2TestRunner;

namespace Repository
{
    public class TestSuiteUtil
    {
        public static DataTable buildTestSuiteList()
        {
            return Utilities.FolderUtil.buildDataTableOfFilesInFolder(Utilities.GlobalFolder.REPOSITORY_TESTSUITE);
        }

        public static void saveTestSuite2File(TestSuite tsItem, string strTestsuiteFilename)
        {
            if (tsItem != null)
            {
                //Initialize DataTable
                DataTable DT_TESTSUITE = new DataTable();
                DT_TESTSUITE.Columns.Add(new DataColumn("Case#", typeof(string)));
                DT_TESTSUITE.Columns.Add(new DataColumn("Detail", typeof(string)));

                //Get Browser & Server out from the last record
                string strBROWSER = tsItem.getTestSuiteBROWSER();
                string strSERVER = tsItem.getTestSuiteSERVER();

                #region loop over all testcase inside the testsuite

                //Get first TEST_CASE
                TestCase tcItem = tsItem.getTestCase();
                int intTCOrder = 0;
                while (tcItem != null)
                {
                    DataRow DR = DT_TESTSUITE.NewRow();
                    string strCaseDetail = "";
                    //Count order of test case (also the number of test case have been executed)
                    ++intTCOrder;

                    //Get first TEST_STEP
                    TestStep teststepItem = tcItem.getTestStep();
                    int intTSOrder = 0;

                    while (teststepItem != null)
                    {
                        #region
                        string strStepDetail = "";
                        string stOwner = teststepItem.getOwner();
                        string stObject = teststepItem.getObjectName();
                        string stAction = teststepItem.getAction();
                        TestParameters tpItem = teststepItem.getParameters();
                        string stValidation = teststepItem.getValidation();

                        //Extract information from tpItem
                        string stParameters = "";
                       
                        for (int i = 0; i < 100; i++)
                        {
                            string strElementDetail = tpItem.getNextItem(i);
                            if (strElementDetail != null)
                            {
                                stParameters += strElementDetail + "+";
                            }
                            else break;
                        }
                        //Remove the last stop point '.'
                        if (stParameters.Length > 0)
                            stParameters = stParameters.Remove(stParameters.Length - 1);

                        //Build stepdetail
                        strStepDetail = stOwner + ";" + stObject + ";" + stAction + ";" + stParameters + ";" + stValidation;

                        //Add stepdetail into a casedetail
                        if (intTSOrder++ == 0)
                            strCaseDetail = strStepDetail;
                        else
                            strCaseDetail += ">" + strStepDetail;

                        //Get next TEST_STEP
                        teststepItem = tcItem.getTestStep();
                        #endregion
                    }

                    //Add DR into DT_TESTSUITE
                    DR[0] = intTCOrder.ToString();
                    DR[1] = strCaseDetail;
                    DT_TESTSUITE.Rows.Add(DR);

                    //Get next TEST_CASE
                    tcItem = tsItem.getTestCase();
                }

                #endregion

                //Add BROWSER & SERVER into DT_TESTSUITE
                DataRow DR_last = DT_TESTSUITE.NewRow();
                DR_last[0] = strBROWSER;
                DR_last[1] = strSERVER;
                DT_TESTSUITE.Rows.Add(DR_last);

                Utilities.DataTableUtil.saveDataTable2File(DT_TESTSUITE, strTestsuiteFilename);
            }
        }

        public static TestSuite getTestSuiteFromFile(string strTestsuiteFilename)
        {
            TestSuite tsItem = new TestSuite();
            DataTable DT_TESTSUITE = Utilities.DataTableUtil.buildDataTableFromFile(strTestsuiteFilename);
            
            //Get browser and server out & set them into tsItem
            int intLastRecord = DT_TESTSUITE.Rows.Count - 1;
            DataRow DR = DT_TESTSUITE.Rows[intLastRecord];
            string strLoadedBrowser = DR[0].ToString();
            string strLoadedServer = DR[1].ToString();
            DT_TESTSUITE.Rows[intLastRecord].Delete();

            tsItem.setTestSuiteBROWSER(strLoadedBrowser);
            tsItem.setTestSuiteSERVER(strLoadedServer);

            //Loop over all row to get testcaseitemdetail
            foreach (DataRow DR_TESTCASE in DT_TESTSUITE.Rows)
            {
                string strTCDetail = DR_TESTCASE[1].ToString();

                TestCase TESTCASE = new TestCase();

                //Extract testcaseitemdetail to get teststepitemdetail
                string[] listTestStep = strTCDetail.Split('>');

                for (int i = 0; i < listTestStep.Length; i++)
                {
                    TestStep TESTSTEP = new TestStep();
                    //Extract teststepitemdetail to get owner,object,action,parameter,validation
                    string[] listElement = listTestStep[i].Split(';');

                    //Build Teststep item
                    TESTSTEP.setOwner(listElement[0]);
                    TESTSTEP.setObjectName(listElement[1]);
                    TESTSTEP.setAction(listElement[2]);
                    TESTSTEP.setValidation(listElement[4]);

                    //Build testparameter
                    string[] listPairKeyValueParameter = listElement[3].Split('+');
                    TestParameters tpItem = new TestParameters();
                    
                    for (int j = 0; j < listPairKeyValueParameter.Length; j++)
                    {
                        if (!listPairKeyValueParameter[j].Equals(""))
                        {
                            string[] pairKeyValueParameter = listPairKeyValueParameter[j].Split('/');
                            tpItem.add(pairKeyValueParameter[0], pairKeyValueParameter[1]);
                        }
                    }

                    //Add testparameter into Teststep
                    TESTSTEP.setParameters(tpItem);

                    //Add Teststep into Testcase item
                    TESTCASE.addTestStep(TESTSTEP);
                }
                tsItem.addTestCase(TESTCASE);
            }
            return tsItem;
        }

        public static DataTable getTestSuiteDataTableGridViewFromFile(string strTestsuiteFilename)
        {
            DataTable dtTarget = new DataTable();
            dtTarget.Columns.Add(new DataColumn("Case#", typeof(string)));
            dtTarget.Columns.Add(new DataColumn("Detail", typeof(string)));

            DataTable DT_TESTSUITE = Utilities.DataTableUtil.buildDataTableFromFile(strTestsuiteFilename);

            //Get browser and server out & set them into tsItem
            int intLastRecord = DT_TESTSUITE.Rows.Count - 1;
            DataRow DR = DT_TESTSUITE.Rows[intLastRecord];
            DT_TESTSUITE.Rows[intLastRecord].Delete();

            //Loop over all row to get testcaseitemdetail
            foreach (DataRow DR_TESTCASE in DT_TESTSUITE.Rows)
            {
                string strTCOrder = DR_TESTCASE[0].ToString();
                string strTCDetail = DR_TESTCASE[1].ToString();
                DataRow DR_dtTarget = dtTarget.NewRow();
                string strDetail = "";

                string[] listTestStep = strTCDetail.Split('>');
                for (int i = 0; i < listTestStep.Length; i++)
                {
                    string[] listElement = listTestStep[i].Split(';');
                    if (i == 0) strDetail = listElement[2];
                    else strDetail += ">" + listElement[2];
                }
                DR_dtTarget[0] = strTCOrder;
                DR_dtTarget[1] = strDetail;
                dtTarget.Rows.Add(DR_dtTarget);
            }
            return dtTarget;
        }
    }
}
